package com.adc.douyu_assistant.utils;

import com.adc.douyu_assistant.entity.DanmuMessage;
import com.adc.douyu_assistant.service.DanmuMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Description
 * @Author lilin
 * @Create 2018-09-14 16:14
 **/
@Component
public class Timer {

    @Autowired
    private DanmuMessageService danmuMessageService;

    public void saveToDB(Map<String, DanmuMessage> danmuMessageMap, long millis) {
        Thread thread = new Thread(new Timer.SaveToDB(danmuMessageMap, millis));
        thread.start();
    }
    class SaveToDB implements Runnable {
        private Map<String, DanmuMessage> danmuMessageMap;
        private long millis;

        public SaveToDB(Map<String, DanmuMessage> danmuMessageMap, long millis) {
            this.danmuMessageMap = danmuMessageMap;
            this.millis = millis;
        }

        public void run() {
            while (true) {
                try {
                    Thread.sleep(millis);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                danmuMessageService.save(danmuMessageMap);
            }
        }
    }
}
